package com.resource.authoritymanage.service.impl;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.resource.authoritymanage.config.TIdentifyRecordEntityListener;
import com.resource.authoritymanage.config.UserPrivilegeEnum;
import com.resource.authoritymanage.entity.TIdentifyApplyEntity;
import com.resource.authoritymanage.entity.TIdentifyAuditEntity;
import com.resource.authoritymanage.entity.TIdentifyRecordEntity;
import com.resource.authoritymanage.entity.TIdentifyRecordEntityExcel;
import com.resource.authoritymanage.mapper.TIdentifyRecordDao;
import com.resource.authoritymanage.service.TIdentifyApplyService;
import com.resource.authoritymanage.service.TIdentifyRecordService;
import com.resource.common.utils.AppResponse;
import com.resource.common.utils.PageUtils;
import com.resource.common.utils.Query;
import com.resource.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


@Service
public class TIdentifyRecordServiceImpl extends ServiceImpl<TIdentifyRecordDao, TIdentifyRecordEntity> implements TIdentifyRecordService {

    @Autowired
    private TIdentifyApplyService tIdentifyApplyService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int size = params.size();
        IPage<TIdentifyRecordEntity> page = new Page<>();
        if (size == 4) {
            String keys = "";
            String param = "";
            Set<String> strings = params.keySet();
            for (String key : strings) {
                keys = key;
                param = params.get(keys).toString();
            }
            page = this.page(
                    new Query<TIdentifyRecordEntity>().getPage(params),
                    new QueryWrapper<TIdentifyRecordEntity>().eq(keys, param)
            );
            return new PageUtils(page);
        }
        page = this.page(
                new Query<TIdentifyRecordEntity>().getPage(params),
                new QueryWrapper<TIdentifyRecordEntity>());
        int sortNo =1;
        for (TIdentifyRecordEntity record : page.getRecords()) {
            record.setSortNo(sortNo++);
        }
        return new PageUtils(page);
    }

    @Override
    public void saveRecord(TIdentifyAuditEntity tIdentifyAudit,String applyNo) {
        TIdentifyRecordEntity tIdentifyRecordEntity = new TIdentifyRecordEntity();
        TIdentifyApplyEntity identifyApply = tIdentifyApplyService.getOne(new QueryWrapper<TIdentifyApplyEntity>().eq("apply_no", applyNo));
        TIdentifyRecordEntity userRecordSelect = this.getOne(new QueryWrapper<TIdentifyRecordEntity>().eq("user_code", applyNo));
        if(!Objects.isNull(identifyApply)){
            String applyType = identifyApply.getApplyType();
            String applyPrivilege = identifyApply.getApplyPrivilege();
            tIdentifyRecordEntity.setUserName(tIdentifyAudit.getApplyName());
            tIdentifyRecordEntity.setUserCode(tIdentifyAudit.getApplyNo());
            tIdentifyRecordEntity.setIsApply("是");
            tIdentifyRecordEntity.setLastLoginTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            tIdentifyRecordEntity.setUserType(applyType);
            tIdentifyRecordEntity.setUserPrivilege(applyPrivilege);
            if(!Objects.isNull(userRecordSelect) && userRecordSelect.getUserCode()!=null){
                this.update(tIdentifyRecordEntity,new QueryWrapper<TIdentifyRecordEntity>().eq("user_code",tIdentifyRecordEntity.getUserCode()));
            }else {
                this.save(tIdentifyRecordEntity);
            }
        }
    }

    @Override
    public R exportData(HttpServletResponse response) {
        try {
            //设置下载信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = "identifyRecord";
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //查询数据库
            List<TIdentifyRecordEntity> tIdentifyRecordEntities = this.baseMapper.selectList(new QueryWrapper<TIdentifyRecordEntity>());
            List<TIdentifyRecordEntityExcel> list = new ArrayList<>();
            int sortNo=1;
            for (TIdentifyRecordEntity tIdentifyRecordEntity : tIdentifyRecordEntities) {
                TIdentifyRecordEntityExcel tIdentifyRecordEntityExcel = new TIdentifyRecordEntityExcel();
                tIdentifyRecordEntityExcel.setSortNo(sortNo+++"");
                tIdentifyRecordEntityExcel.setIsApply(tIdentifyRecordEntity.getIsApply());
                tIdentifyRecordEntityExcel.setUserCode(tIdentifyRecordEntity.getUserCode());
                tIdentifyRecordEntityExcel.setUserName(tIdentifyRecordEntity.getUserName());
                tIdentifyRecordEntityExcel.setUserType(tIdentifyRecordEntity.getUserType());
                tIdentifyRecordEntityExcel.setUserPrivilege(tIdentifyRecordEntity.getUserPrivilege());
                tIdentifyRecordEntityExcel.setLastLoginTime(tIdentifyRecordEntity.getLastLoginTime());
                list.add(tIdentifyRecordEntityExcel);
            }
            EasyExcel.write(response.getOutputStream(), TIdentifyRecordEntityExcel.class).sheet("权限记录表").doWrite(list);
            return R.ok("导出成功");
        }catch (IOException e){
            e.printStackTrace();
        }
        return R.error("导出错误");
    }

    @Override
    public R importData(@RequestParam("file") MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),TIdentifyRecordEntityExcel.class,new TIdentifyRecordEntityListener(baseMapper)).sheet("权限记录表").doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  R.ok();
    }
}